package cn.iocoder.yudao.module.crm.service.fuelingrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crm.controller.admin.fuelingrecord.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.fuelingrecord.FuelingRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crm.dal.mysql.fuelingrecord.FuelingRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;

/**
 * 加油记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FuelingRecordServiceImpl implements FuelingRecordService {

    @Resource
    private FuelingRecordMapper fuelingRecordMapper;

    @Override
    public Long createFuelingRecord(FuelingRecordSaveReqVO createReqVO) {
        // 插入
        FuelingRecordDO fuelingRecord = BeanUtils.toBean(createReqVO, FuelingRecordDO.class);
        fuelingRecordMapper.insert(fuelingRecord);
        // 返回
        return fuelingRecord.getId();
    }

    @Override
    public void updateFuelingRecord(FuelingRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateFuelingRecordExists(updateReqVO.getId());
        // 更新
        FuelingRecordDO updateObj = BeanUtils.toBean(updateReqVO, FuelingRecordDO.class);
        fuelingRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteFuelingRecord(Long id) {
        // 校验存在
        validateFuelingRecordExists(id);
        // 删除
        fuelingRecordMapper.deleteById(id);
    }

    private void validateFuelingRecordExists(Long id) {
        if (fuelingRecordMapper.selectById(id) == null) {
            throw exception(FUELING_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public FuelingRecordDO getFuelingRecord(Long id) {
        return fuelingRecordMapper.selectById(id);
    }

    @Override
    public PageResult<FuelingRecordDO> getFuelingRecordPage(FuelingRecordPageReqVO pageReqVO) {
        return fuelingRecordMapper.selectPage(pageReqVO);
    }

}